define(function (require,exports,module) {
    require("./flyRequest.css");
    var sTpl = require("./template.html");
    var VueComponent = Vue.extend({
        template: sTpl,
        props:['parentData'],
        methods:{
            submitUpload:function() {
                var $this= this;
                this.sizeForm.uploadFileType="request";

                this.$refs.form.validate(function(val){
                    if(val){
                        if(!$this.sizeForm.fileList[0].raw){
                            $this.$notify({"title":"未选区文件",message:"请先选取文件"});
                            return;
                        }
                        if($this.$refs.upload.fileList.length>0){
                            $this.$refs.upload.fileList[0].status="ready";
                        }
                        $this.$refs.upload.submit();

                    }
                })

            },
            submitData:function () {
                var $this= this;
                this.$refs.form.validate(function(val){
                    if(val){
                        axios.post("/dam/api/flyRequest/update",$this.sizeForm).then(function (data) {
                            $this.$notify({"title":"修改成功",message:"修改成功"})
                        });

                    }
                })
            },
            submitBakUpload:function(){

                this.sizeForm.uploadFileType="bak";
                if(!this.sizeForm.messageId){
                    this.$notify({"title":"上传失败",message:"请先上传申请文件"});
                    return;
                }
                var $this = this;
                this.$refs.bakForm.validate(function(val){
                if(val){
                    if(!$this.sizeForm.bakFileList[0].raw){
                        this.$notify({"title":"未选区文件",message:"请先选取文件"});
                        return;
                    }
                    if($this.$refs.uploadBak.fileList.length>0){
                        $this.$refs.uploadBak.fileList[0].status="ready";
                    }
                    $this.$refs.uploadBak.submit();
                }
            })
            }
            ,
            handleRemove:function(file, fileList) {
                console.log(file, fileList);
            },
            handlePreview:function(file) {
                console.log(file);
            },
            handleChange:function(file, fileList) {
                if(fileList.length==2){
                    fileList=fileList.slice(1);
                    this.sizeForm.fileList=fileList;
                    this.$refs.upload.uploadFiles.splice(0,1);
                }
                if(file.status=="ready"){
                    this.sizeForm.fileList=fileList;

                }

            },
            handleBakChange:function(file, fileList) {
                if(fileList.length==2){
                    fileList=fileList.slice(1);
                    this.sizeForm.fileList=fileList;
                    this.$refs.uploadBak.uploadFiles.splice(0,1);
                }
                if(file.status=="ready"){
                    this.sizeForm.bakFileList=fileList;

                }

            },
            uploaded:function(file, fileList) {
                if(!this.parentData||!this.parentData.edit){
                    this.sizeForm.fileList=[];
                    this.sizeForm.messageId='';
                    this.sizeForm.bakFileList=[];
                    this.$refs.form.resetFields();
                    this.$refs.bakForm.resetFields();
                }
                 this.$notify({"title":"上传成功",message:"文件上传成功"});
            },
            "uploadedSuccess":function(response, file, fileList){
                   var filList = this.sizeForm.fileList;
                   var bakFileList = this.sizeForm.bakFileList;
                   this.sizeForm=response;
                   this.sizeForm.fileList=filList;
                   this.sizeForm.bakFileList=bakFileList;
                   this.$notify({"title":"上传成功",message:"文件上传成功"});
            },
            pickDate:function (val) {
                this.sizeForm.messageDate=val;
            }

        },
        data:function(){
                return {
                    sizeForm: {
                        messageId:'',
                        messageDate: '',
                        description:'',
                        messageType:'flyRequest',
                        status:'',
                        name:'',
                        bakName:'',
                        uploadFileType:'request',
                        fileList: [],
                        bakFileList:[]
                    },
                    rules:{
                        messageDate: [{ required: true, message: '请选择日期', trigger: 'blur' }],
                        description:[  { required: true, message: '请输入描述', trigger: 'blur' },
                                         {  max: 20, message: '长度不能超过20', trigger: 'blur' }],
                        status:[{ required: true, message: '状态不能为空', trigger: 'blur' }],
                        fileList: [{validator:function (rule, value, callback) {
                          if(value.length!=1){
                              return callback(new Error("请选取执飞文件"));
                          }if(value[0].name.length>30){
                                return callback(new Error("文件名不能超过30"));
                            }
                          return callback();
                        }, trigger: 'blur' }],
                        bakFileList: [{validator:function (rule, value, callback) {
                            if(value.length!=1){
                                return callback(new Error("请选取执飞回执文件"));
                            }
                            if(value[0].name.length>30){
                                return callback(new Error("文件名不能超过30"));
                            }
                            return callback();
                        }, trigger: 'blur' }]
                    },
                }
            },
        activated:function () {
            messages.$emit('change','执飞申请录入');
        }

    });

    module.exports = VueComponent;
});

